package test.solr;

import java.io.IOException;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.params.CursorMarkParams;

import com.alibaba.fastjson.JSON;

public class Cursor {
	
	public static void main(String[] args) {
		HttpSolrClient client = new HttpSolrClient("http://localhost:8080/solr/core2");
		SolrQuery query = new SolrQuery("*:*");
		query.addField("title");
		query.setRows(10);
		query.addSort("score", ORDER.desc);
		query.addSort("id", ORDER.desc);
		query.setParam(CursorMarkParams.CURSOR_MARK_PARAM, CursorMarkParams.CURSOR_MARK_START);
		try {
			QueryResponse qr = client.query(query);
			System.out.println(JSON.toJSONString(qr.getResponse().asMap(Integer.MAX_VALUE), true));
			
			String cursorMark = (String) qr.getResponse().get(CursorMarkParams.CURSOR_MARK_NEXT);
			query.setParam(CursorMarkParams.CURSOR_MARK_PARAM, cursorMark);
			qr = client.query(query);
			System.out.println(JSON.toJSONString(qr.getResponse().asMap(Integer.MAX_VALUE), true));
			
			client.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SolrServerException e) {
			e.printStackTrace();
		}
	}

}
